<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="服务名称">
        <template slot-scope="scope">
          {{ scope.row.serviceName }}
        </template>
      </el-table-column>
      <el-table-column label="服务ip">
        <template slot-scope="scope">
          {{ scope.row.ip }}
        </template>
      </el-table-column>
      <el-table-column label="服务端口">
        <template slot-scope="scope">
          {{ scope.row.port }}
        </template>
      </el-table-column>
      <el-table-column label="权重">
        <template slot-scope="scope">
          {{ scope.row.weight }}
        </template>
      </el-table-column>
      <el-table-column label="监控地址">
        <template slot-scope="scope">
          <a :href="scope.row.monitorUrl" target="_blank" style="color: #4AB7BD">{{ scope.row.monitorUrl }}</a>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
  import {create, getPageDruidMonitor, update} from "@/api/druidMonitor";

  export default {
    filters: {
      statusFilter(status) {
        const statusMap = {
          1: 'success',
          2: 'info',
          0: 'danger'
        }
        return statusMap[status]
      }
    },
    data() {
      return {
        list: null,
        listLoading: true,
        downloadLoading: false,
        total: 0,
        listQuery: {
          page: 1,
          limit: -1,
          importance: undefined,
          label: undefined,
          type: undefined,
          sort: 'id'
        },
        dialogFormVisible: false,
        dialogStatus: '',
        textMap: {
          update: '编辑',
          create: '新增'
        },
        dialogPvVisible: false,
        temp: {
          serviceName: '',
          ip: '',
          port: '',
          weight: '',
          monitorUrl: '',
        },
        columnLabel: {
          serviceName: '服务名称',
          ip: '服务ip',
          port: '服务端口',
          weight: '权重',
          monitorUrl: '监控地址',
        },
      }
    },
    created() {
      this.getList()
    },
    methods: {
      getList() {
        this.listLoading = true
        getPageDruidMonitor(this.listQuery).then(response => {
            this.total = response.data.total
            this.list = response.data.records
            // Just to simulate the time of the request
            setTimeout(() => {
              this.listLoading = false
            }, 1 * 1000)
          },
          error => {
          })
      },
      handleFilter() {
        this.listQuery.page = 1
        this.getList()
      },
      statusFilter(status) {
        const statusMap = {
          1: '启用',
          0: '禁用'
        }
        return statusMap[status]
      },
      resetTemp() {
        this.temp = {
          serviceName: '',
          ip: '',
          port: '',
          weight: '',
          monitorUrl: '',
        }
      },
      handleCreate() {
        this.resetTemp(),
          this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      createData() {
        this.$refs['dataForm']
          .validate((valid) => {
            if (valid) {
              create(this.temp).then(() => {
                this.list.unshift(this.temp)
                this.dialogFormVisible = false
                this.$notify({
                  title: '成功',
                  message: '创建成功',
                  type: 'success',
                  duration: 2000
                }),
                  this.getList()
              })
            }
          })
      },
      handleUpdate(row) {
        this.temp = Object.assign({}, row) // copy obj
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      updateData() {
        this.$refs['dataForm']
          .validate((valid) => {
            if (valid) {
              const tempData = Object.assign({}, this.temp)
              update(tempData).then(() => {
                const index = this.list.findIndex(v => v.id === this.temp.id)
                this.list.splice(index, 1, this.temp)
                this.dialogFormVisible = false
                this.$notify({
                  title: '成功',
                  message: '更新成功',
                  type: 'success',
                  duration: 2000
                }),
                  this.getList()
              })
            }
          })
      },
      handleDelete(row) {
        const h = this.$createElement;
        this.$msgbox({
          title: '消息',
          message: h('p', null, [
            h('span', null, '确认要删除当前记录吗？ ')
          ]),
          showCancelButton: true,
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(action => {
          const tempData = {'id': row.id, 'isDelete': 1}
          update(tempData).then(() => {
            const index = this.list.findIndex(v => v.id === this.temp.id)
            this.list.splice(index, 1, this.temp)
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            }),
              this.getList()
          })
        });
      }
    }
  }
</script>
